package br.com.syshealth.clinica.session;

import java.util.List;
import java.util.Set;


import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.criterion.Example;

import br.com.syshealth.clinica.entity.Agenda;
import br.com.syshealth.clinica.entity.Medico;
import br.com.syshealth.clinica.entity.Pessoa;

@Stateless
public class AgendaDAO {
	
	
	
	@EJB
	private MedicoDAO medicoDAO;
	@PersistenceContext(unitName="clinica-syshealth-core")
	private EntityManager em;
//	private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("aluno");
	
	
	public void atualizar(Agenda bean){
		Object ent = em.merge(bean);
		System.out.println("atualizando");
		em.persist(ent);
	}
	
    public void salvar(Agenda bean){
    	
    	
    	
    	em.persist(bean);	
    	
    }
	
	public void deletar( Agenda bean) {
		System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
//		Object ent = em.merge(bean);
//		em.remove(ent);
		Query query = em.createQuery("delete from Agenda  where id="+bean.getId());
		query.executeUpdate();
		
	}
	
	public Agenda getBean( int id) {
		
		
		return em.find(Agenda.class, id);
	}
	
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public void montaAgenda(Agenda agenda)
	{
		
		agenda.setMedico(medicoDAO.getBean(agenda.getMedico().getCodPessoa()));
		
		System.out.println(agenda.getMedico().getNome());
		
		agenda.getMedico().getListAgenda().add(agenda);
		em.persist(agenda);
		 
		
		
		

		
		
	}
	
	
	public void vinculaMedico(Agenda agenda){
		
		Query query = em.createQuery("update clinica.agenda set id_medico="+agenda.getMedico().getCodPessoa()+" where id="+agenda.getId());
		query.executeUpdate();
		
		System.out.println("********************************************** ADDDDDDDDDDDDD################");
		
	}
	
	
	public List<Agenda> pesquisaConsultas(Agenda agenda)
	{
		List<Agenda> agendaList = null;
		try{
			Query query = em.createQuery("select p from Agenda p where id_medico="+agenda.getMedico().getCodPessoa()+" and data='"+(agenda.getData().getYear()+1900)+"-"+(agenda.getData().getMonth()+1)+"-"+agenda.getData().getDate()+"' order by hora_inicio");
			agendaList = query.getResultList();
		 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return agendaList;
		
	}
	
	public List<Agenda> pesquisaConsultasDisponiveis(Agenda agenda)
	{
		List<Agenda> agendaList = null;
		try{
			Query query = em.createQuery("select p from Agenda p where id_medico="+agenda.getMedico().getCodPessoa()+" and data='"+(agenda.getData().getYear()+1900)+"-"+(agenda.getData().getMonth()+1)+"-"+agenda.getData().getDate()+"' and ativo='false' order by hora_inicio");
			agendaList = query.getResultList();
		 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return agendaList;
		
	}
	
	public List<Agenda> pesquisaConsultasMarcadas(Agenda agenda)
	{
		List<Agenda> agendaList = null;
		try{
			Query query = em.createQuery("select p from Agenda p where id_medico="+agenda.getMedico().getCodPessoa()+" and data='"+(agenda.getData().getYear()+1900)+"-"+(agenda.getData().getMonth()+1)+"-"+agenda.getData().getDate()+"' and ativo='true' order by hora_inicio");
			agendaList = query.getResultList();
		 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return agendaList;
		
	}
	
	public List<Agenda> pesquisaConsultasPaciente(Agenda agenda)
	{
		List<Agenda> agendaList = null;
		try{
			Query query = em.createQuery("select p from Agenda p where paciente="+agenda.getPaciente().getCodPessoa()+" and data='"+(agenda.getData().getYear()+1900)+"-"+(agenda.getData().getMonth()+1)+"-"+agenda.getData().getDate()+"' and ativo='true' order by hora_inicio");
			agendaList = query.getResultList();
		 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return agendaList;
		
	}
	
	public List<Agenda> pesquisaConsultasMedico(Agenda agenda)
	{
		List<Agenda> agendaList = null;
		try{
			Query query = em.createQuery("select p from Agenda p where id_medico="+agenda.getPaciente().getCodPessoa()+" and data='"+(agenda.getData().getYear()+1900)+"-"+(agenda.getData().getMonth()+1)+"-"+agenda.getData().getDate()+"' and ativo='true' order by hora_inicio");
			agendaList = query.getResultList();
		 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return agendaList;
		
	}

}
